package com.example;

import com.example.mapper.DemoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Classname Service1Impl
 * @Description TODO
 * @Date 2021/4/28 21:50
 * @Created by yin
 */
@Service

public class Service1Impl implements Service1 {
    @Autowired
    DemoMapper demoMapper;

//    新建一个事务，原事务暂时挂起
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    public void transaction1() throws Exception {
        demoMapper.add("yin",100);
        throw new Exception("报错");
    }

//    内部事务
    @Transactional(propagation = Propagation.NESTED)
    @Override
    public void transaction2() throws Exception{
        demoMapper.add("yin",100);
        throw new Exception("报错");
    }

//    直接使用外部事务
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void transaction3() throws Exception{
        demoMapper.add("yin",100);
        throw new Exception("报错");
    }
}
